//
//  TopicImageTableViewCell.m
//  XiangMuB
//
//  Created by 向修根 on 16/8/13.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "TopicImageTableViewCell.h"
#import "TopocModel.h"
#import "DataBaseHandle.h"

#import <SDWebImage/UIImageView+WebCache.h>


#import "MBProgressHUD.h"


@interface TopicImageTableViewCell ()

@property (strong,nonatomic) MBProgressHUD *HUD;



@end

@implementation TopicImageTableViewCell


- (void)layoutSubviews{
    [super layoutSubviews];
    self.name.text = self.model.name;
    self.classification.text = self.model.classification;
    NSString *str = [NSString stringWithFormat:@"%@",self.model.concernCount];
    self.concernCount.text = [str stringByAppendingString:@"关注"];
    
    NSString *str1 = [NSString stringWithFormat:@"%@",self.model.talkCount];
    self.talkCount.text = [str1 stringByAppendingString:@"讨论"];
    NSArray *array = self.model.talkPicture;
    
    [self.image1 sd_setImageWithURL:array[0] placeholderImage:nil];
    [self.image2 sd_setImageWithURL:array[1] placeholderImage:nil];
    [self.image3 sd_setImageWithURL:array[2] placeholderImage:nil];
    
    //    弧度
    self.cellView.layer.cornerRadius = self.cellView.frame.size.width /50;
    self.cellView.layer.masksToBounds = YES;
    
    //   cell边框
    self.cellView.layer.borderWidth = 2;
    self.cellView.layer.borderColor = [[UIColor colorWithRed:192.0f/255.0f  green:192.0f/255.0f  blue:192.0f/255.0f  alpha:0.7]CGColor];
    [[DataBaseHandle shareDataBaseHandle]openDatabase];
    NSArray *array1 = [[DataBaseHandle shareDataBaseHandle]select];
    NSString *str3 = [NSString string];
    for (NSDictionary *dic in array1) {
        str3 = dic[@"name"];
        if ([str3 isEqualToString:self.name.text]) {
            [self.foucesButton setTitle:@"已关注" forState:(UIControlStateNormal)];
            self.foucesButton.backgroundColor = [UIColor whiteColor];
        }
    }
    
   

    
}

- (IBAction)fouce:(id)sender {

    if ([self.foucesButton.titleLabel.text isEqualToString:@"+关注"]) {
        [[DataBaseHandle shareDataBaseHandle]openDatabase];
        [[DataBaseHandle shareDataBaseHandle]CreateTable];
        [[DataBaseHandle shareDataBaseHandle]insterTableWithModel:self.model];
        [[DataBaseHandle shareDataBaseHandle]closeDatabase];
        
        [self.foucesButton setTitle:@"已关注" forState:(UIControlStateNormal)];
        self.foucesButton.backgroundColor = [UIColor whiteColor];
        
        self.HUD = [[MBProgressHUD alloc]initWithView:self.cellView];
        [self.cellView addSubview:self.HUD];
        self.HUD.label.text = @"收藏成功";
        self.HUD.mode = MBProgressHUDModeText;
        
        [self.HUD showAnimated:YES whileExecutingBlock:^{
            sleep(2);
        } completionBlock:^{
            [self.HUD removeFromSuperview];
            self.HUD = nil;
        }];
        
    }else{
        [[DataBaseHandle shareDataBaseHandle]openDatabase];
        [[DataBaseHandle shareDataBaseHandle]deleteWithModle:self.model];
        [[DataBaseHandle shareDataBaseHandle]closeDatabase];
        [self.foucesButton setTitle:@"+关注" forState:(UIControlStateNormal)];
        self.foucesButton.backgroundColor = [UIColor redColor];
        
        self.HUD = [[MBProgressHUD alloc]initWithView:self.cellView];
        [self.cellView addSubview:self.HUD];
        self.HUD.label.text = @"取消收藏";
        self.HUD.mode = MBProgressHUDModeText;
        
        [self.HUD showAnimated:YES whileExecutingBlock:^{
            sleep(2);
        } completionBlock:^{
            [self.HUD removeFromSuperview];
            self.HUD = nil;
        }];
    }

    
    
}




- (void)awakeFromNib {
    [self.name NightWithType:UIViewColorTypeClear];
    
    [self.classification NightWithType:UIViewColorTypeClear];
    [self.concernCount NightWithType:UIViewColorTypeClear];
    [self.talkCount NightWithType:UIViewColorTypeClear];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
